package com.sam.im.heiqun.callmain;

import android.graphics.Point;
import android.util.Log;
import android.view.Window;

import com.sam.im.heiqun.nets.PGApi;
import com.sam.im.heiqun.utils.PrivateUtil;
import com.sam.im.heiqun.app.App;

import org.webrtc.AudioSource;
import org.webrtc.CameraEnumerationAndroid;
import org.webrtc.MediaConstraints;
import org.webrtc.MediaStream;
import org.webrtc.PeerConnection;
import org.webrtc.VideoCapturerAndroid;
import org.webrtc.VideoSource;

import java.util.LinkedList;

/**
 * Created by jack on 2017-07-15.
 */

public class RtcUtil {

    private static final String VIDEO_CODEC_VP9 = "VP9";
    private static final String AUDIO_CODEC_OPUS = "opus";
    public static int FONT_FACTING = 0 ;
    public static int BACK_FACING = 1 ;
    private static VideoCapturerAndroid videoCapturerAndroid;

    public static LinkedList<PeerConnection.IceServer> getIceServers(){
        LinkedList<PeerConnection.IceServer> iceServers=new LinkedList<PeerConnection.IceServer>();
//        PeerConnection.IceServer server=new PeerConnection.IceServer("turn:120.27.145.73:3478","jack9527","123654");
        PeerConnection.IceServer server=new PeerConnection.IceServer("turn:"+ PGApi.getFWUrl+":3478","jack9527","123654");
        iceServers.add(server);
        iceServers.add(new PeerConnection.IceServer("stun:"+ PGApi.getFWUrl+":3478"));
        return iceServers;
    }

    public static MediaConstraints getPcConstraints(){
        MediaConstraints pcConstraints=new MediaConstraints();
        pcConstraints.mandatory.add(new MediaConstraints.KeyValuePair("OfferToReceiveAudio", "true"));
        pcConstraints.mandatory.add(new MediaConstraints.KeyValuePair("OfferToReceiveVideo", "true"));
        pcConstraints.optional.add(new MediaConstraints.KeyValuePair("DtlsSrtpKeyAgreement", "true"));
        return pcConstraints;
    }

    public static PeerConnectionParameters getConnParameter(Window context){
        Point displaySize = new Point();
        context.getWindowManager().getDefaultDisplay().getSize(displaySize);
        PeerConnectionParameters params = new PeerConnectionParameters(
                true, false, displaySize.x, displaySize.y, 12, 1, VIDEO_CODEC_VP9, true, 1, AUDIO_CODEC_OPUS, true);

        return params;
    }

    public static void startCamera(RtcListener listener,PeerConnectionParameters pcParams, VideoSource videoSource ,int type){
        MediaStream localMS = App.pcFactory.createLocalMediaStream("ARDAMS");
        if(pcParams.videoCallEnabled){
            MediaConstraints videoConstraints = new MediaConstraints();
            videoConstraints.mandatory.add(new MediaConstraints.KeyValuePair("maxHeight", Integer.toString(pcParams.videoHeight)));
            videoConstraints.mandatory.add(new MediaConstraints.KeyValuePair("maxWidth", Integer.toString(pcParams.videoWidth)));
            videoConstraints.mandatory.add(new MediaConstraints.KeyValuePair("maxFrameRate", Integer.toString(pcParams.videoFps)));
            videoConstraints.mandatory.add(new MediaConstraints.KeyValuePair("minFrameRate", Integer.toString(pcParams.videoFps)));
            String cameraname = "";
            if (type == FONT_FACTING){
                cameraname = CameraEnumerationAndroid.getNameOfFrontFacingDevice(); //前置摄像头
            }else {
                cameraname = CameraEnumerationAndroid.getNameOfBackFacingDevice(); //后置摄像头
            }
            videoCapturerAndroid = (VideoCapturerAndroid) VideoCapturerAndroid.create(cameraname);
            /*videoCapturerAndroid = VideoCapturerAndroid.create(cameraname, new VideoCapturerAndroid.CameraEventsHandler() {
                @Override
                public void onCameraError(String s) {

                }

                @Override
                public void onCameraFreezed(String s) {

                }

                @Override
                public void onCameraOpening(int i) {

                }

                @Override
                public void onFirstFrameAvailable() {

                }

                @Override
                public void onCameraClosed() {

                }
            });*/
            //videoSource = pcFactory.createVideoSource(VideoCapturerAndroid.create(cameraname), videoConstraints);
            videoSource = App.pcFactory.createVideoSource(videoCapturerAndroid, videoConstraints);
            localMS.addTrack(App.pcFactory.createVideoTrack("ARDAMSv0", videoSource));
        }
        AudioSource audioSource = App.pcFactory.createAudioSource(new MediaConstraints());
        localMS.addTrack(App.pcFactory.createAudioTrack("ARDAMSa0", audioSource));
        App.pc.addStream(localMS);//???
        listener.onLocalStream(localMS);
    }
    public static void switchCamera(){
        videoCapturerAndroid.switchCamera(new VideoCapturerAndroid.CameraSwitchHandler() {
            @Override
            public void onCameraSwitchDone(boolean b) {

            }

            @Override
            public void onCameraSwitchError(String s) {

            }
        });


    }
    public static void disposd(){
        try {
            PrivateUtil.invoke(videoCapturerAndroid, "stopCapture");
           // Method stopCaptureOnCameraThread = PrivateUtil.getMethod(videoCapturerAndroid.getClass(), "stopCaptureOnCameraThread", new Class[]{});
           // Log.i("info","==="+stopCaptureOnCameraThread);
        }catch (Exception e){

        }

    }
    public static void createOffer(RtcSdpObserver observer,MediaConstraints constraints){
        Log.i("info","createOffer===");
        App.pc.createOffer(observer,constraints);
    }

}
